导航菜单
首页 >  Creating AI Assistants with GPT  > Build your own AI assistant in 10 lines of code

Build your own AI assistant in 10 lines of code

This tutorial will guide you to develop a chat assistant with gpt-3.5-turbo accessible via the console.

Prerequisites

Before starting, you will need to have:

Python 3.7.1 or higher installed on your systemAn OpenAI API keyStep 1: Install the OpenAI Python Library

First, we need to install the latest Python client library (currently v0.27.0) for the OpenAI API. You can install it using pip, the Python package manager, with the following command:

pip install openaiStep 2: Set Up Your OpenAI API Key

You will need to set up an OpenAI API key. If you don’t already have one, you can get one by following the instructions provided in the OpenAI API documentation.

Once you have your API key, replace "YOUR_API_KEY" in the code snippet below with your API key:

import openaiopenai.api_key = "YOUR_API_KEY"Step 3: Write the system message

The system message is a message object with the "role" : "system". The system message helps set the behavior of the assistant. You can also give the assistant a name using the system message.

When creating your own chat assistant, it’s important to choose a good directive prompt. Here are some tips to keep in mind:

Keep it simple and clear: The directive should be clear, intentional, concise and declarative.

Set the tone: The directive should establish the chat assistant’s personality and tone. This can be done through the choice of words, phrasing, and even punctuation. For example, a chat assistant that is meant to be friendly and approachable might use exclamation points and emojis, while one that is meant to be more serious and professional might use more formal language and punctuation.

Test and iterate: Don’t be afraid to experiment with different directive prompts and see how users respond. You may need to tweak the language or tone of the prompt to get the best results.

Replace the "DIRECTIVE_FOR_gpt-3.5-turbo" placeholder in next step (4) with the system message you wrote.

Step 4: Create the Chat Assistant

Now that we have the OpenAI Python library installed and our API key set up, we can create the chat assistant.

import openaiopenai.api_key = "YOUR_API_KEY" # supply your API key however you choosemessage = {"role":"user", "content": input("This is the beginning of your chat with AI. [To exit, send \"###\".]\n\nYou:")};conversation = [{"role": "system", "content": "DIRECTIVE_FOR_gpt-3.5-turbo"}]while(message["content"]!="###"):conversation.append(message)completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=conversation) message["content"] = input(f"Assistant: {completion.choices[0].message.content} \nYou:")print()conversation.append(completion.choices[0].message)Step 5: Run the Chat Assistant

To run the chat assistant, save the code snippet from step 4

Conclusion

In this tutorial, I’ve shown you how to create a chat assistant using the OpenAI Python library and the GPT-3.5-turbo model. I’ve also discussed the importance of the system directive in establishing the chat assistant’s personality and tone, and provided some tips for creating a good directive prompt.

Remember that chat assistant development is an ongoing process, and you may need to experiment with different approaches and iterate on your design to get the best results. Good luck, and happy coding!

相关推荐: